package be.nepherte.movmanager.client.ui.components;

import java.util.ArrayList;
import java.util.List;

import be.nepherte.movmanager.domain.Movie;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Composite;

/**
 * This class defines all the required methods for a particular {@link Movie
 * movies} view. A movies view may contain multiple movies and is navigatable.
 * 
 * <p>
 * Copyright © 2010 Bart Verhoeven
 * </p>
 * 
 * <p>
 * This file is part of MovManager.
 * </p>
 * 
 * <p>
 * MovManger is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * </p>
 * 
 * <p>
 * MovManger is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * </p>
 * 
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * MovManger. If not, see http://www.gnu.org/licenses/.
 * </p>
 * 
 * @author Bart Verhoeven
 * @version 0.1
 */
public abstract class AbstractMoviesView extends Composite implements HasMovies, Navigatable {
	/**List of movies in this movies view.*/
	protected List<Movie> movies = new ArrayList<Movie>();
	/**Index of the first visible movie from <tt>movies</tt> in this movies view.*/
	protected int firstMovieIndex;
	/**Maximum number of visible movies in this movies view.*/
	protected int maxVisibleEntries;
	
	/**
	 * Creates a new movies view.
	 * 
	 * @param	movies
	 * 			list of movies in this view
	 */
	protected AbstractMoviesView(List<Movie> movies) {
		this.movies = movies;
	}

	@Override
	public abstract void navigateLeft();

	@Override
	public abstract void navigateRight();

	@Override
	public abstract HasClickHandlers getLeftNavigationElement();

	@Override
	public abstract HasClickHandlers getRightNavigationElement();

	@Override
	public abstract void setMaxVisibleElements(int i);

	@Override
	public abstract void setMovies(List<Movie> movies);
	
	/**
	 * Sets all movies and displays them, starting from <tt>firstMovieIndex</tt>
	 * . All previous movies are removed. When movies is <tt>null</tt>, no
	 * movies are displayed.
	 * 
	 * @param	movies
	 * 			list of new movies to be displayed
	 * @param	firstMovieIndex
	 * 			index of the first movie in <tt>movies</tt> to be displayed
	 */
	public abstract void setMovies(List<Movie> movies, int firstMovieIndex);
	
	@Override
	public List<Movie> getMovies() {
		return movies;
	}
	
	@Override
	public int getMaxVisibleElements() {
		return maxVisibleEntries;
	}
	
	/**
	 * Retrieves the index of the first displayed movie in the movie list.
	 * 
	 * @return	firstMovieIndex;
	 */
	public int getFirstMovieIndex() {
		return firstMovieIndex;
	}
	
	/**
	 * Calculates the index of the first movie to be displayed, given some movie
	 * index, incorporating the maximum number of visible elements.
	 * 
	 * @param	movieIndex
	 * @return	index of the first movie to be displayed given a movie index and
	 *        	maximum number of visible elements
	 */
	public int calculateFirstMovieIndex(int movieIndex) {
		return (movieIndex/maxVisibleEntries)*maxVisibleEntries;
	}
	
	public void setFirstMovieIndex(int movieIndex) {
		firstMovieIndex = movieIndex;
	}
}
